﻿<%@ WebHandler Language="C#" Class="PrintServiceCharge" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class PrintServiceCharge : AjaxHandlerBase {

    public PrintServiceCharge()
    {
        htTypePermit.Add("printview", Constants.Permit_Service_PrintView);
        htTypePermit.Add("reprintview", Constants.Permit_Service_RePrintView);
    }

    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //个人业绩排行
                case "getprintmodel":
                    strRet = GetPrintModel(context);
                    break;
                case "saveprint":
                    strRet = SavePrintInfo(context);
                    break;
                default:
                    break;
            }
            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    private string SavePrintInfo(HttpContext context)
    {
        string billId = context.Request["billId"];
        Chenxin.Lib.Business.Entity.t_Intention_BillPrintInfo mode = new Chenxin.Lib.Business.Entity.t_Intention_BillPrintInfo();
        mode.RecordId = Guid.NewGuid();
        mode.IntentionId = new Guid(billId);
        mode.IntentionCode = context.Request["billCode"];
        mode.PrintTime = DateTime.Now;
        mode.PrintEmployee = CurrentUserInfo.UserName + "(" + CurrentUserInfo.UserCode + ")";
        mode.PrintEmployeeId = CurrentUserInfo.UserId;
        mode.BillType = 20;

        Chenxin.Lib.Business.DAL.t_Intention_BillPrintInfo.Add(mode);

        string text = CurrentUserInfo.UserName + "：服务费打印。服务费收据：" + mode.IntentionCode;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_PrintService"].ToString());

        return retExecResult(Constants.ExecResult.Succ, "成功", "");
    }

    /// <summary>
    /// 获取Bill
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    private string GetPrintModel(HttpContext context)
    {
        string billId = (string.IsNullOrEmpty(context.Request["billId"])) ? Guid.Empty.ToString() : context.Request["billId"];

        if (string.IsNullOrEmpty(billId)) return retExecResult(Constants.ExecResult.Error, "开单标识丢失", "");
        Chenxin.Lib.Business.Entity.v_PrintServiceInfo model = Chenxin.Lib.Business.DAL.v_PrintServiceInfo.GetModel(new Guid(billId));
        if (model == null) return retExecResult(Constants.ExecResult.Error, "获取服务费信息失败，请联系管理员！", "");

        model.PrintTime = DateTime.Now.ToShortDateString();
        string str = "";
        Chenxin.Lib.Utility.InitialHelper.TranslateNumberToChinese(model.GetedMoney.ToString(), out str);
        model.GetedMoneyBig = str;

        string strSql = "IntentionId='" + billId + "'";

        List<Chenxin.Lib.Business.Entity.t_Intention_BillPrintInfo> list = Chenxin.Lib.Business.DAL.t_Intention_BillPrintInfo.GetListArray(strSql);
        model.PrintCount = list.Count+1;

        //获得服务费款项List        
        StringBuilder sb = new StringBuilder();

        List<Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo> itemslist = Chenxin.Lib.Business.DAL.ServiceChargePaymodeInfo.GetListArray(" ReceiptId='" + model.ReceiptId + "'");
        sb.Append("<table width=\"100%\" cellspacing=\"1\" style=\"margin-left:15px;\" class=\"tabsorternew table_archive\">");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:center;width:15%;font-size:13px;\">收取方式</th><th style=\"text-align:center;width:15%;font-size:13px;\">金额</th><th style=\"text-align:center;width:15%;font-size:13px;\">交款编号</th><th style=\"text-align:center;font-size:13px;width:15%;\">备注</th>");
        sb.Append("</tr>");

        if (itemslist.Count < 1)
        {
            sb.Append("<tr><td colspan=\"4\" style=\"text-align:center;height: 25px;font-size:13px;\">无款项明细记录</td></tr>");
        }
        else
        {
            foreach (Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo item in itemslist)
            {
                sb.Append("<tr>");
                sb.AppendFormat("<td style=\"text-align:center;font-size:13px;height: 25px;\">{0}</td><td style=\"text-align:center;font-size:13px;\">{1}</td><td style=\"text-align:center;font-size:13px;\">{2}</td><td style=\"text-align:center;font-size:13px;\">{3}</td>", item.PayMode, item.PayMoney, item.PayCode, item.Memo);
                sb.Append("</tr>");
            }
        }
        sb.Append("</table>");
        
        model.Details = sb.ToString();

        return string.Format("{{success:1,msg:'',data:{{model:{0}}}}}", JsonHelp.Convert2Json(model));
    }

}